package org.wisdom.ab.apis.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.wisdom.ab.apis.CompanyController;
import org.wisdom.ab.apis.assembler.CompanyAssembler;
import org.wisdom.ab.domain.company.entity.AbCompany;
import org.wisdom.ab.domain.company.repository.CompanyRepository;
import org.wisdom.ab.domain.department.entity.AbDepartment;
import org.wisdom.ab.domain.department.repository.DepartmentRepository;
import org.wisdom.ab.dto.CompanyDto;
import org.wisdom.annotation.WisdomController;
import org.wisdom.exception.BusinessException;
import org.wisdom.vo.PageModel;
import org.wisdom.vo.ResponseResult;

import lombok.RequiredArgsConstructor;

/**
 * 公司管理API
 *
 * @author 张智研
 * @date 2022/05/20 14:50
 **/
@RequiredArgsConstructor
@WisdomController
public class CompanyControllerImpl implements CompanyController {

    private final CompanyRepository companyRepository;
    private final CompanyAssembler companyAssembler;
    private final DepartmentRepository departmentRepository;

    @Override
    public ResponseResult<String> save(CompanyDto companyDto) {
        AbCompany company = this.companyAssembler.companyDtoToEntity(companyDto);
        company = this.companyRepository.save(company);
        return ResponseResult.ok(company.getId());
    }

    @Override
    public ResponseResult<CompanyDto> get(String companyId) {
        Optional<AbCompany> companyOptional = this.companyRepository.findById(companyId);
        if (companyOptional.isPresent()) {
            CompanyDto companyDto = this.companyAssembler.companyEntityToDto(companyOptional.get());
            return ResponseResult.ok(companyDto);
        } else {
            return ResponseResult.fail("公司{" + companyId + "}不存在!");
        }
    }

    @Override
    public ResponseResult<String> delete(String companyIds) {
        List<String> ids = Arrays.stream(companyIds.split(",")).collect(Collectors.toList());
        for (String companyId : ids) {
            List<AbDepartment> departmentList = this.departmentRepository.findAllByCompanyId(companyId);
            if (!departmentList.isEmpty()) {
                throw new BusinessException("公司" + companyId + "存在部门，不允许删除！");
            }else {
                this.companyRepository.deleteById(companyId);
            }
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<PageModel<CompanyDto>> list(CompanyDto companyDto) {
        AbCompany company = this.companyAssembler.companyDtoToEntity(companyDto);
        PageModel<CompanyDto> pageModel = new PageModel<>();
        if(companyDto.getIsPage()) {
            Page<AbCompany> companyPage =
                    this.companyRepository.findAll(company, companyDto.getPageNumber() - 1, companyDto.getPageSize());
            List<CompanyDto> companyDtoList = this.companyAssembler.companyEntityToDto(companyPage.getContent());
            pageModel.setContent(companyDtoList);
            pageModel.setPageSize(companyPage.getSize());
            pageModel.setPageNumber(companyPage.getNumber() + 1);
            pageModel.setTotalPages(companyPage.getTotalPages());
            pageModel.setTotalElements(companyPage.getTotalElements());
        }else {
            List<AbCompany> companyList = this.companyRepository.findAll(company);
            List<CompanyDto> companyDtoList = this.companyAssembler.companyEntityToDto(companyList);
            pageModel.setContent(companyDtoList);
            pageModel.setPageSize(companyList.size());
            pageModel.setPageNumber(1);
            pageModel.setTotalPages(1);
            pageModel.setTotalElements(companyList.size());
        }
        return ResponseResult.ok(pageModel);
    }
}
